package com.huawei.esdk.uc.device.bmu.userlevel;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.bean.commu.RestReqMessage;
import com.huawei.esdk.platform.commu.itf.ISDKProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.uc.device.AbstractBMUCapability;
import com.huawei.esdk.uc.device.bean.XMLResMsg;
import com.huawei.esdk.uc.device.bmu.IUserLevelCapability;
import com.huawei.esdk.uc.device.bmu.bean.QueryUserLevelResponse;
import com.huawei.esdk.uc.device.bmu.userlevel.convert.UserLevelCapabilityConvert;
import com.huawei.esdk.uc.domain.model.UserLevel;

public class UserLevelCapabilityImpl extends AbstractBMUCapability implements IUserLevelCapability
{
    private static final Logger LOGGER = Logger.getLogger(UserLevelCapabilityImpl.class);
    
    private UserLevelCapabilityConvert userLevelCapabilityConvert = new UserLevelCapabilityConvert();
    
    public UserLevelCapabilityImpl(ISDKProtocolAdapter protocolAdapter)
    {
        super(protocolAdapter);
    }

    @Override
    public SDKResult<UserLevel> queryUserLevel(String userId, String pageCount, String pageNum)
    {
        LOGGER.debug("queryUserLevel() start");
        SDKResult<UserLevel> sdkResult = new SDKResult<UserLevel>();
        
        RestReqMessage request = userLevelCapabilityConvert.getQueryUserLevelRequest(userId, pageCount, pageNum);
        
        try
        {
            XMLResMsg result = sendMessage(request, "queryUserLevel.action", QueryUserLevelResponse.class.getName());
            sdkResult.setErrCode(Integer.valueOf(result.getHead().getRetCode()));
            sdkResult.setDescription(result.getHead().getRetContext());
            
            if (0 == sdkResult.getErrCode())
            {
                sdkResult.setResult(userLevelCapabilityConvert.getQueryUserLevelResponse((QueryUserLevelResponse)result.getBody()));
            }
        }
        catch (ProtocolAdapterException e)
        {
            sdkResult.setErrCode(e.getErrorCode());
            LOGGER.error("queryUserLevel() error", e);
        }
        
        LOGGER.debug("queryUserLevel() end");
        
        return sdkResult;
    }
  
}
